package com.minhphuong.controllers;

import com.minhphuong.entities.*;
import com.minhphuong.bll.*;

import java.util.List;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author QuocMinh
 */
@Controller
public class SanPhamController {

    @RequestMapping(value = "/ChiTietSanPham/{id}", method = RequestMethod.GET)
    public String index(ModelMap map, HttpSession session, @PathVariable int id) {
        HelperBLL.LoadGiaoDien(map, session);

        SanPham sanPham = SanPhamBLL.getSanPhamByID(id);
        map.addAttribute("sanPham", sanPham);

        List<SanPham> listSanPham = SanPhamBLL.getAllSanPham();

        //List<SanPham> listSanPham = SanPhamBLL.getTopSanPham(6);
        map.addAttribute("listSanPham", listSanPham);

        return "chitietsanpham";

    }

    @RequestMapping(value = "/SanPham/", method = RequestMethod.GET)
    public String SanPham(ModelMap map, HttpSession session) {
        HelperBLL.LoadGiaoDien(map, session);

        return "sanpham";
    }

    @RequestMapping(value = "/SanPham/{tenHang}", method = RequestMethod.GET)
    public String SanPhamHang(ModelMap map, HttpSession session, @PathVariable String tenHang) {
        HelperBLL.LoadGiaoDien(map, session);

        Hang hang = SanPhamBLL.getAllSanPhamByHang(tenHang);
        map.addAttribute("hang", hang);

        return "sanphamhang";
    }

    @RequestMapping(value = "/TimKiem/", method = RequestMethod.GET)
    public String TimKiem(ModelMap map, HttpSession session,
            @RequestParam("search_category") String search_category,
            @RequestParam("search_price") String search_price,
            @RequestParam("search_productname") String search_productname,
            @RequestParam(value = "page", required = false) String _page,
            @RequestParam(value = "start", required = false) String _start) {
        HelperBLL.LoadGiaoDien(map, session);

        List<SanPham> listSanPhamCount = SanPhamBLL.getAllSanPhamByTimKiem(search_category, search_price, search_productname);

        /////////////////
        int display = 9;
        int current = 0;
        int next = 0;
        int prev = 0;
        int record = 0;
        int page = 0;
        int start = 0;
        //tính tổng số trang cần hiện thị
        if (_page != null) {
            page = Integer.parseInt(_page);
        } else {
            record = listSanPhamCount.size();
            if (record > display) {
                page = (int) Math.ceil(record*1.0 / display);
            } else {
                page = 1;
            }
        }
        if (_start != null) {
            start = Integer.parseInt(_start);
        } else {
            start = 0;
        }

        List<SanPham> listSanPham = SanPhamBLL.getAllSanPhamByTimKiemLimit(search_category, search_price, search_productname, start, display);
        map.addAttribute("listSanPham", listSanPham);

        if (page > 1) {
            next = start + display;
            prev = start - display;
            current = (start / display) + 1;
        }

        map.addAttribute("record", record);
        map.addAttribute("current", current);
        map.addAttribute("display", display);
        map.addAttribute("page", page);
        map.addAttribute("prev", prev);
        map.addAttribute("next", next);

        ////////////////
        return "timkiem";
    }
}
